package service

import (
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/models"
	"MyBlogv2/blog-common/response"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func GetRoleList(c *gin.Context) {
	var roleList []models.RoleAllVo
	models.Db.Model(new(models.RoleAllVo)).Find(&roleList)
	response.ResponseOK(c, roleList)
	return
}

func UpdateRoleStatus(c *gin.Context) {
	var updateRoleStatusDto models.UpdateRoleStatusDto
	if err := c.ShouldBindJSON(&updateRoleStatusDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.Role)).Where("id = ?", updateRoleStatusDto.Id).Update("status", updateRoleStatusDto.Status)
	response.ResponseOK(c, nil)
	return
}

func UpdateRole(c *gin.Context) {
	var roleDto models.RoleDto
	if err := c.ShouldBindJSON(&roleDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.RoleDto)).Where("id = ?", roleDto.Id).Updates(roleDto)
	sql := "delete from sys_role_menu where role_id = ?"
	models.Db.Exec(sql, roleDto.Id)
	for _, v := range roleDto.MenuIds {
		var roleMenu models.RoleMenu
		roleMenu.RoleId = roleDto.Id
		roleMenu.MenuId = v
		models.Db.Model(new(models.RoleMenu)).Create(&roleMenu)
	}
	response.ResponseOK(c, nil)
}

func GetRoleById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var role models.RoleByIdVo
	models.Db.Model(new(models.RoleByIdVo)).Where("id = ?", id).Find(&role)
	var menuIds []int
	models.Db.Model(new(models.RoleMenu)).Select("menu_id").Where("role_id = ?", id).Find(&menuIds)
	role.MenuIds = menuIds
	response.ResponseOK(c, role)
}

func DeleteRoleByIds(c *gin.Context) {
	var roleDeleteDto models.RoleDeleteDto
	if err := c.ShouldBindJSON(&roleDeleteDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.Role)).Where("id in (?)", roleDeleteDto.Ids).Delete(new(models.Role))
	response.ResponseOK(c, nil)
}

func SearchRole(c *gin.Context) {
	var roleALlVo []models.RoleAllVo
	var roleSearchDto models.RoleSearchDto
	if err := c.ShouldBindJSON(&roleSearchDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	db := models.Db.Model(new(models.RoleAllVo))
	if roleSearchDto.RoleName != "" {
		db.Where("role_name like ?", "%"+roleSearchDto.RoleName+"%")
	}
	if roleSearchDto.RoleKey != "" {
		db.Where("role_key like ?", "%"+roleSearchDto.RoleKey+"%")
	}
	if roleSearchDto.Status.Valid {
		db.Where("status = ?", roleSearchDto.Status.Int64)
	}
	if !time.Time(roleSearchDto.CreateTimeStart).IsZero() {
		db.Where("created_at >= ?", roleSearchDto.CreateTimeStart)
	}
	if !time.Time(roleSearchDto.CreateTimeEnd).IsZero() {
		db.Where("created_at <= ?", roleSearchDto.CreateTimeEnd)
	}
	db.Find(&roleALlVo)
	response.ResponseOK(c, roleALlVo)
	return

}
func AddRole(c *gin.Context) {
	var roleDto models.RoleDto
	if err := c.ShouldBindJSON(&roleDto); err != nil {
		response.ResponseError(c, HttpCode.PARAM_ERROR)
		return
	}
	models.Db.Model(new(models.RoleDto)).Create(&roleDto)
	for _, v := range roleDto.MenuIds {
		var roleMenu models.RoleMenu
		roleMenu.RoleId = roleDto.Id
		roleMenu.MenuId = v
		models.Db.Model(new(models.RoleMenu)).Create(&roleMenu)
	}
	response.ResponseOK(c, nil)
	return
}
